package com.ts.proj.service.impl;

import com.ts.base.service.impl.CommonServiceImpl;
import com.ts.proj.entity.SystemFieldEntity;
import com.ts.proj.repository.SystemFieldRepository;
import com.ts.proj.service.SystemFieldService;
import com.ts.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class SystemFieldServiceImpl extends CommonServiceImpl<SystemFieldEntity> implements SystemFieldService {

    @Autowired
    private SystemFieldRepository systemFieldRepository;



    @Override
    public List<SystemFieldEntity> findAll(PageData pageData, Integer start, Integer length) {
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        //使用 CriteriaBuilder方式来做过滤
        Specification<SystemFieldEntity> sp = this.getSpecification(pageData,SystemFieldEntity.class);
        List<SystemFieldEntity> all = systemFieldRepository.findAll(sp,pageable).getContent();
        return all;
    }

    @Override
    public long count(PageData pageData) {
        return systemFieldRepository.count(this.getSpecification(pageData,SystemFieldEntity.class));
    }

    @Override
    public boolean deleteByIds(Iterable<SystemFieldEntity> iterable) {
        systemFieldRepository.deleteInBatch(iterable);
        return false;
    }

    @Override
    public List<SystemFieldEntity> saveAll(List<SystemFieldEntity> columnEntities) {
        return systemFieldRepository.saveAll(columnEntities);
    }

    @Override
    public boolean deleteAll() {
        return false;
    }

    @Override
    public List<SystemFieldEntity> findAllById(Integer[] ids) {
        List<SystemFieldEntity> list = systemFieldRepository.findAllById(Arrays.asList(ids));
        if(list.size()==0){
            SystemFieldEntity systemFieldEntity = new SystemFieldEntity();
            list.add(systemFieldEntity);
        }
        return list;
    }

    @Override
    public List<String> findFieldTable() {
        return systemFieldRepository.findFieldTable();
    }
}
